package com.honour.controller.interfaced;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.impl.ReplyInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.PageData;
import com.system.util.Tools;

/**
 @Function
 @Author zhaozhenhua
 @Date   2017年12月9日
*/

@Controller
@RequestMapping(value="/appreplyin")
public class ReplyInController extends BaseController{
	
	@Resource(name="replyinService")
	private ReplyInService replyinService;
	
	/**
	 * 给评论回复
	 * @Author zhaozhenhua
	 * @date   2017年12月9日
	 * @return
	 */
	@RequestMapping(value="/addreply")
	@ResponseBody
	public Object addComment(){
		logBefore(logger, "给评论回复");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("REPLYADD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addReply", pd)){	//检查参数
					pd.put("REPLY_ID", this.getSnowId());
					//发布时间
					Date date = new Date();
					String format = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
					pd.put("REPLYTIME", format);
					//获取评论数
					replyinService.save(pd);
					result = (null == pd) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 通过评论ID获取所有回复
	 * @Author zhaozhenhua
	 * @date   2017年12月9日
	 * @return
	 */
	@RequestMapping(value="/replylist")
	@ResponseBody
	public Object getCommentList(){
		logBefore(logger, "通过评论ID获取所有回复");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("REPLYLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("replyList", pd)){	//检查参数
					List<PageData> list = replyinService.getCommentReplyListById(pd);
					if (list.size()!=0) {
						map.put("pd", list);
					}
					result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
}
